<?php
/**
 * 后台管理
 * @author 天酷 QQ:593244214
 * @copyright 天酷网络&copy;2011 http://bbs.8eoo.com
 */
class AdminAction extends Action{
	var $user;
	
	function init(){
		UserModel::isLogin(true);
		$usertype=intval(getInf('USER_TYPE'));
		if ($usertype!=99){
			$this->Message("抱歉！你的权限不足");
		}else{
			$this->user=Model('User');
			$u=$this->user->getInfo(intval(getInf('USER_ID')));
			$this->assign('my', $u);
		}
	} 
	function cmdIndex(){
		$this->assign('logtime', getInf('LOGIN_TIME'));
		$this->assign("title", "后台管理");
		$this->display("admin/index.htm");
	}

	function cmdUserList(){
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$ulist=$this->user->getList($page,$pagesize);
		$this->assign('ul', $ulist['data']);
		$this->assign('count', $ulist['count']);
		$this->assign('page', $page);
		$this->assign('pagecount',$ulist['pagecount']);
		$this->assign('pagesize', $pagesize);
		$this->assign('title', '用户管理');
		$this->display('admin/userlist.htm');
	}

	function cmdUseradmin(){
		$id=isset($_GET['id'])?$_GET['id']:0;
		$this->assign('u', $this->user->getInfo($id));
		$this->assign('title', '用户资料编辑');
		$this->display('admin/useredit.htm');
	}

	function cmdUsersave(){
		global $_SID;
		$userid=isset($_POST['userid'])?$_POST['userid']:0;
		$username=isset($_POST['username'])?trim($_POST['username']):null;
		$usernick=isset($_POST['usernick'])?trim($_POST['usernick']):null;
		$userhead=isset($_POST['userhead'])?trim($_POST['userhead']):null;
		//$password=isset($_POST['password'])?trim($_POST['password']):null;
		$password2=isset($_POST['password2'])?trim($_POST['password2']):null;
		$phone=isset($_POST['phone'])?trim($_POST['phone']):null;
		$sex=isset($_POST['sex'])?trim($_POST['sex']):null;
		$usermobile=isset($_POST['usermobile'])?trim($_POST['usermobile']):null;

		$msg=null;
		$user=new UserModel();
		$userinfo=$user->getInfo($userid);
		//if (strlen($username)<2)$msg.=$userna.'用户名不能为空或小于2字符⊙﹏⊙b汗<br />';
		if ($usernick=='' || strlen($usernick)<2)$msg.="昵称不能为空或小于2字符\(^o^)/~<br />";
		//if ($password!=$userinfo['user_pass'])$msg.="旧密码输入不正确\O(∩_∩)O~<br />";
		if ($password2==''){$password2=$userinfo['user_pass'];}else{$password2=md5($password2);}
		if (strlen($phone)!=11 || !is_numeric($phone))$msg.="手机号码必须为11位数字<br />";
		if (empty($msg)){
			@$result=$user->Update($username, $usernick,$password2,$phone,$sex,$usermobile,$userhead,$userid);
			if ($result=='OK'){
				$this->Message("修改成功","恭喜修改成功",url(array($GLOBALS['METHOD']=>'userlist')),3);
			}else{
				$this->Message($result,'修改出错',url(array($GLOBALS['METHOD']=>'userlist')),3);
			}
		}else{
			$this->Message($msg,'修改出错',url(array($GLOBALS['METHOD']=>'userlist')),3);
		}
	}
	
	function cmdUserdel(){
		$del=isset($_GET['del'])?$_GET['del']:null;
		$userid=isset($_GET['id'])?$_GET['id']:null;
		$userid=verify_id($userid);
		$user = new UserModel();
		$m=$user->getInfo($userid);
		$this->assign('m', $m);
		$this->assign('title', '删除'.$m['user_name']);
		if ($del){
			$result=$user->DelUser($userid);//str_replace('Maodel','Maolist',getUrl(false))
			$this->Message($result,'提示',url(array($GLOBALS['METHOD']=>'userlist')),3);
		}else{
			$this->display('admin/userdel.htm');
		}
	}
	
	function cmdMao(){
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$mao=new MaoModel();
		$m=$mao->getSmsList($page,$pagesize);
		$this->assign("m", $m['data']);
		$this->assign('count', $m['count']);
		$this->assign('page', $page);
		$this->assign('pagecount', $m['pagecount']);
		$this->assign('pagesize', $pagesize);
		$this->assign('title', '管理短信猫');
		$this->display('admin/maolist.htm');
	}
	
	function cmdMaolist(){
		$user=isset($_GET['id'])?$_GET['id']:null;
		$mao=new MaoModel();
		$m=$mao->getMaolist($user);
		$this->assign('m', $m['data']);
		$this->assign('title', '指令列表');
		$this->display('admin/commandlist.htm');
	}
	
	function cmdMaoedit(){
		$maoid=isset($_GET['id'])?$_GET['id']:null;
		$maoid=verify_id($maoid);
		$mao=new MaoModel();
		$m=$mao->getMao($maoid);
		$year = date('Y');
		$years = $days = array();
		for($i = $year; $i < $year + 50; $i++){
			$years[] = $i; 
		}
        $dayCount = date('t',strtotime($m['command_expired']));
        for($j = 1 ; $j <= $dayCount; $j++){
            if($j < 10){
                $days[] = '0'.$j;
            }else{
                $days[] = $j;
            }
        }

		$this->assign('years', $years);
        $this->assign('days', $days);
        $this->assign('months', array('01','02','03','04','05','06','07','08','09','10','11','12'));

        $t = explode(' ',$m['command_expired']);
        $this->assign('expi',explode('-',$t[0]));

		$this->assign('m', $m);

		$this->assign('title', '修改短信猫配置');
		$this->display('admin/maoedit.htm');
	}
	
	function cmdMaosave(){
		$maoid=isset($_POST['maoid'])?$_POST['maoid']:null;
		$maoid=verify_id($maoid);
		$userid=isset($_POST['userid'])?$_POST['userid']:null;
		$maourl=isset($_POST['maourl'])?$_POST['maourl']:null;
		$maokey=isset($_POST['maokey'])?$_POST['maokey']:null;
		$maomsg=isset($_POST['maomsg'])?$_POST['maomsg']:null;
		$year = isset($_POST['year'])?$_POST['year']:date('Y');
		$mthon = isset($_POST['mthon'])?$_POST['mthon']:date('m');
		$day = isset($_POST['day'])?$_POST['day']:date('d');
		$status=(is_numeric($_POST['status']) && in_array($_POST['status'], array(1,2)))?$_POST['status']:null;
		$maourl=str_check($maourl);
		$maokey=str_check($maokey);
		$maomsg=str_check($maomsg);
		if (!is_numeric($day))$msg = '日期格式不正确';
		if (!is_numeric($userid))$msg="用户ID必须为数字";
		if (strlen($maomsg)<2)$msg="短信指令不能少于2字符";
		if (strlen($maokey)<6)$msg="通信密令不能少于6字符";
		if (substr($maourl, 0,7)!="http://")$msg="注册接口不正确";
		if (isset($msg)){
			$this->Message($msg);
		}else{
			$expired = $year.'-'.$mthon.'-'.$day.' '.date('H:i:s');
			$mao=new MaoModel(); 
			$return=$mao->updateMao($maoid, $maourl, $maomsg, $maokey, $userid, $expired, $status);
			$this->Message($return,'提示信息',url(array($GLOBALS['METHOD']=>'Maolist')),3);
		}
	}
	/**
	 * 删除短信猫
	 * Enter description here ...
	 */
	function cmdMaodel(){
		$del=isset($_GET['del'])?$_GET['del']:null;
		$maoid=isset($_GET['id'])?$_GET['id']:null;
		$maoid=verify_id($maoid);
		$mao=new MaoModel();
		$m=$mao->getMao($maoid);
		$this->assign('m', $m);
		$this->assign('title', '删除'.$m['mao_msg']);
		if ($del){
			$result=$mao->delMao($maoid);//str_replace('Maodel','Maolist',getUrl(false))
			$this->Message($result,'提示',url(array($GLOBALS['METHOD']=>'Maolist')),3);
		}else{
			$this->display('admin/maodel.htm');
		}
	}
	
	function cmdLog(){
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$log=new LogModel();
		$list=$log->getList(null,null,2,$page,$pagesize);
		$this->assign('count', $list['count']);
		$this->assign('page', $page);
		$this->assign('pagecount', $list['pagecount']);
		$this->assign('pagesize', $pagesize);
		$this->assign('list', $list['data']);
		$this->assign('title', "所有日志");
		$this->display('admin/alllog.htm');
	}
	
	function cmdLoglist(){
		$page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
		$page=verify_id($page);
		$pagesize=10;
		
		$id=isset($_GET['id'])?$_GET['id']:null;
		$id=verify_id($id);
		$log=new LogModel();
		$l=$log->getList($id,null,1,$page,$pagesize);
		$this->assign('count', $l['count']);
		$this->assign('page', $page);
		$this->assign('id', $id);
		$this->assign('pagecount', $l['pagecount']);
		$this->assign('pagesize', $pagesize);
		$this->assign("l", $l['data']);
		$this->assign('title', '日志查看');
		$this->display('admin/loglist.htm');
	}
	/**
	 * 留言管理
	 * Enter description here ...
	 */
	function cmdLy(){
		$ly=new LyModel();
		$this->assign('title', "留言管理");
		$this->assign('ly', $ly->getLy());
		$this->display("admin/ly.htm");
		
	}
	
	function cmdArticle(){
		$article=new ArticleModel();
		$page=isset($_GET['page'])?$_GET['page']:1;
		$pagesize=10;
		
		if($page)verify_id($page);
		$list=$article->getList(null,$page,$pagesize);
		$this->assign('list', $list['data']);
		$this->assign('page', $page);
		$this->assign('pagesize', $pagesize);
		$this->assign('count', $list['count']);
		$this->assign('title', '文章管理');
		$this->assign('pagecount',$list['pagecount']);
		$this->display('admin/article_list.htm');
	}
	
	function cmdArticleEdit(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$id=verify_id($id);
		
		$article=new ArticleModel();
		$this->assign('id', $id);
		$this->assign('view',$article->getView($id));
		$this->assign('title', '编辑文章');
		$this->display('admin/article_edit.htm');
	}
	
	function cmdArticleSave(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$id=verify_id($id);
		$title=str_check($_POST['title']);
		$content=str_check($_POST['content']);
        $msg = '';
		
		if(strlen($title)<5)$msg.="文章标题太短<br />";
		if(strlen($content)<10)$msg.="文章内容太少<br />";
		
	
		if(strlen($msg)==0){
			$article=new ArticleModel();
			$article->UpdateArticle($title, $content, $id);
			$this->Message("文章已成功保存","保存完成",url(array($GLOBALS['METHOD']=>'Article')),3);
		}else{
			$this->Message($msg,"保存出错",str_replace("ArticleSave", "ArticleEdit", getUrl(false)),3);
		}
	}
	
	function cmdArticleDel(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$id=verify_id($id);
		$v=new ArticleModel();
		$this->assign('view', $v->getView($id));
		$this->assign('title', '删除文章');
		$this->display("admin/article_del.htm");
	}
	
	function cmdArticleDels(){
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:null;
		$id=verify_id($id);
		$v=new ArticleModel();
		$v->DelArticle($id);
		$this->Message("文章已删除",null,url(array($GLOBALS['METHOD']=>'Article')),3);
	}
	
	function cmdArticleAdd(){
		$this->assign('title', '添加文章');
		$this->display("admin/article_edit.htm");
	}
	
	function cmdArticleAdds(){
		$title=str_check($_POST['title']);
		$content=str_check($_POST['content']);
		$msg="";
		if(strlen($title)<5)$msg.="文章标题太短<br />";
		if(strlen($content)<10)$msg.="文章内容太少<br />";
		if(strlen($msg)==0){
			$article=new ArticleModel();
			$article->SaveArticle($title, $content,1);
			$this->Message("文章已保存","保存成功",str_replace("ArticleAdds", "Article", getUrl(false)),3);
		}else{
			$this->Message($msg,"保存出错",str_replace("ArticleAdds", "ArticleAdd", getUrl(false)),3);
		}
	}
	/**
	 * 网站设置
	 */
	function cmdSiteConfig(){
		$act = isset($_GET['act']) ? $_GET['act'] : null;
		if(!$act){
			$this->assign('site', SiteConfig::getAll());
			$this->assign('title','网站设置');
			$this->display('admin/site_config.htm');
		}else if($act == 'save'){
			$sitename = isset($_POST['sitename']) ? qstr($_POST['sitename']) :null;
			$monthmoney = isset($_POST['month_money']) ? qstr($_POST['month_money']) : null; 
			$auth_code = isset($_POST['auth_code']) ? qstr($_POST['auth_code']) : null; 
			$bugemail = isset($_POST['bug_email']) ? qstr($_POST['bug_email']) : null; 
			$sendmail = isset($_POST['send_mail']) ? qstr($_POST['send_mail']) : null;
            $license_key = isset($_POST['license_key']) ? qstr($_POST['license_key']) : null;

            if(empty($sitename) || empty($monthmoney) || !is_numeric($monthmoney)){
				$this->Message("请输入正确的网站名或每月收费","保存失败",str_replace("act", "", getUrl(false)),3);
			}else{
				$site = SiteConfig::getInstance();
				$site->set('site_name',$sitename)
				->set('month_money',$monthmoney)
				->set('bug_email',$bugemail)
				->set('send_mail',$sendmail)->set('license_key',$license_key);
				if(!empty($auth_code))$site->set('auth_code',md5($auth_code));
				$site->save();
				$this->Message("网站设置已保存","保存成功",url(array($GLOBALS['METHOD']=>'Index')),3);
			}
		}
	}
	/**
	 * 邮件配置
	 */
	function cmdMailConfig(){
		$act = isset($_GET['act']) ? $_GET['act'] : null;
		if(!$act){
			$this->assign('mail', SiteConfig::getAll());
			$this->assign('title', '邮件发送设置');
			$this->display('admin/mail_config.htm');
		}else if($act=='save'){
			$smtpserver = isset($_POST['smtpserver']) ? qstr($_POST['smtpserver']) : null; 
			$smtpauth = isset($_POST['smtpauth']) ? qstr($_POST['smtpauth']) : null; 
			$smtp_charset = isset($_POST['smtp_charset']) ? qstr($_POST['smtp_charset']) : null; 
			$smtp_user = isset($_POST['smtp_user']) ? qstr($_POST['smtp_user']) : null; 
			$smtp_pass = isset($_POST['smtp_pass']) ? qstr($_POST['smtp_pass']) : null; 
			$smtp_from = isset($_POST['smtp_from']) ? qstr($_POST['smtp_from']) : null; 
			
			if(empty($smtpserver) || empty($smtp_charset)){
				$this->Message("请输入正确的smtp信息","保存失败",str_replace("act", "", getUrl(false)),3);
			}else{
				$site = SiteConfig::getInstance();
				$site->set('smtp_server',$smtpserver)
				->set('smtp_auth',$smtpauth)->set('smtp_charset',$smtp_charset)
				->set('smtp_user',$smtp_user)->set('smtp_pass',$smtp_pass)
				->set('smtp_from',$smtp_from)->save();
				$this->Message("邮件设置已保存","保存成功",url(array($GLOBALS['METHOD']=>'Index')),3);
			}
		}
		
	}
	/**
	 * 金币管理
	 */
    function cmdMoneyManager(){
        $act = isset($_GET['act']) ? $_GET['act'] : null;
        $userId = isset($_REQUEST['uid']) ? intval($_REQUEST['uid']) : null;
        $optionType = isset($_REQUEST['optionType']) ? intval($_REQUEST['optionType']) : null;
        $number = isset($_REQUEST['number']) ? intval($_REQUEST['number']) : null;
        if(!$act){
            $this->assign('optionType',$optionType);
            $this->assign('userid', $userId);
            $this->assign('title', L('MONEY_NAME').'充值');
            $this->display('admin/money_manager.htm');
        }else if($act=='save'){
            if(empty($userId) || empty($optionType) || empty($number) || !is_numeric($userId) || !is_numeric($optionType) || !is_numeric($number)){
                $this->Message('请输入正确的数据','操作失败',str_replace("act", "", getUrl(false)),33);
            }else{
                $user = new UserModel();
                $rt = $user->moneyManager($userId,$optionType,$number);
                if($rt){
                    $this->Message('操作成功!','操作成功',url(array($GLOBALS['METHOD']=>'Index')),3);
                }else{
                    $this->Message('请检查用户是否存在!','操作失败',str_replace("act", "", getUrl(false)),3);
                }
            }
        }
    }
    /**
     * 注册配置
     */
    function cmdRegisterConfig() {
    	$act = isset($_GET['act']) ? $_GET['act'] : null;
    	if(!$act){
    		$this->assign('site', SiteConfig::getAll());
    		$this->assign('title', '注册设置');
    		$this->display('admin/register_config.htm');
    	}else if($act == 'save'){
    		$regType = (isset($_POST['regType']) && is_numeric($_POST['regType'])) ? intval($_POST['regType']) : null;
    		$smsKey = isset($_POST['smskey']) ? str_check($_POST['smskey']) : null;
    		$giftMoney = (isset($_POST['gift_money']) && is_numeric($_POST['gift_money'])) ? intval($_POST['gift_money']) : null;
    		$sms_recv = isset($_POST['sms_recv']) ? str_check($_POST['sms_recv']) : null;
    		
    		$site = SiteConfig::getInstance();
    		$site->set('sms_key',$smsKey)
    		->set('gift_money',$giftMoney)
    		->set('sms_recv_phone',$sms_recv)
    		->set('reg_type',$regType)->save();
    		$this->Message('操作成功!','操作成功',url(array($GLOBALS['METHOD']=>'Index')),3);
    	}
    }
}